home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C15 / Wind3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  757 b   |  37 lines

  1. //: C15:Wind3.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Late binding with virtual
  7. #include <iostream>
  8. using namespace std;
  9. enum note { middleC, Csharp, Cflat }; // Etc.
  10.  
  11. class Instrument {
  12. public:
  13.   virtual void play(note) const {
  14.     cout << "Instrument::play" << endl;
  15.   }
  16. };
  17.  
  18. // Wind objects are Instruments
  19. // because they have the same interface:
  20. class Wind : public Instrument {
  21. public:
  22.   // Redefine interface function:
  23.   void play(note) const {
  24.     cout << "Wind::play" << endl;
  25.   }
  26. };
  27.  
  28. void tune(Instrument& i) {
  29.   // ...
  30.   i.play(middleC);
  31. }
  32.  
  33. int main() {
  34.   Wind flute;
  35.   tune(flute); // Upcasting
  36. } ///:~
  37.